import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:study_flutter/router/Application.dart';
import 'package:study_flutter/router/Routes.dart';
import 'package:study_flutter/syh/SConstant.dart';

import 'RConstant.dart';
import 'dyc/DConstant.dart';
import 'lyc/LConstant.dart';
import 'wyfg/WConstant.dart';
import 'yzhg/ProvidesInfo.dart';
import 'yzhg/YConstant.dart';

/// 学习文档 ： https://jspang.com/detailed?id=53#toc2237
///
/// Flutter Toast                                https://github.com/PonnamKarthik/FlutterToast
/// banner轮播图解决方案                           https://github.com/best-flutter/flutter_swiper
/// 屏幕适配解决方案                               https://github.com/OpenFlutter/flutter_screenutil
/// 打电话,发短信，邮件，打开Http插件               https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher
/// Toast 用法                                   https://github.com/PonnamKarthik/FlutterToast
/// dio网络请求                                  https://github.com/flutterchina/dio
/// sp储存                                      https://dart-pub.mirrors.sjtug.sjtu.edu.cn/packages/shared_preferences_web#-readme-tab-
/// 下拉刷新 上拉加载控件                        https://github.com/xuelongqy/flutter_easyrefresh
/// JSON TO DART(将json文件转为实体)            https://javiercbk.github.io/json_to_dart/
/// Flutter EventBUs                          https://github.com/marcojakob/dart-event-bus
/// Flutter 路由跳转                          https://pub.dev/packages/fluro
/// Flutter Html插件（有问题）                https://github.com/Sub6Resources/flutter_html
/// Flutter Html插件2                       https://github.com/zhaolongs/Flutter_Fai_Webview

/*
 * Main函数，程序的入口。main函数中调用了MyApp方法。
 * 功能时开启Flutter应用
 *    runApp它接受一个widget参数。myApp()是Flutter应用的根组件
 *
 *    main函数使用=>符号，这是dart中单行函数或者方法的简写
 */

void main() {
  //处理全局状态管理
  var provides = Providers();
  // provides..provide(Provider<Counter>.value(Counter()));
  //获取 处理全局状态管理
  var providesInfo = new ProvidesInfo();
  //加载  全局状态管理类
  providesInfo.addProvides(provides);
  //配置 状态管理
  runApp(ProviderNode(child: MyApp(), providers: provides));
}

class MyApp extends StatelessWidget {

  /*在构造里面处理  路由三方框架*/
  MyApp(){
    //创建一个router对象
    final router = Router();
    //配置Router注册管理
    Routes.configureRoutes(router);
    //将生成的router给全局化
    Application.router = router;
  }

  
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      title: 'Flutter实践', //应用的名称
      theme: ThemeData(
        //应用的主题
        //设置主题颜色为蓝色
        primarySwatch: Colors.blue,
      ),
      //注册路由表
      routes: RConstant.getRoutes(context),
      //主页面的路由。
      home: MyHomePage(title: 'Flutter Demo Home Page'),
      //第三方框架在这里生成路由
      onGenerateRoute: Application.router.generator,
    );
  }


}

/*
 * 设置 数据
 *
 * 图片大小  ： 400 * 400 像素
 */
const MODULE_LIST = [
  {
    "title": "论英雄 · 谁主沉浮",
    "name": "杨义",
    "image": "assets/images/yzhg_modul_logoe.png",
    "navigator": "yzhg"
  },
  {
    "title": "学如不及 · 犹恐失之",
    "name": "杜亚超",
    "image": "assets/images/yzhg_modul_logoe.png",
    "navigator": "dyc"
  },
  {
    "title": "言之无文 · 行而不远",
    "name": "李勇聪",
    "image": "assets/images/yzhg_modul_logoe.png",
    "navigator": "lyc"
  },
  {
    "title": "知而好问 · 然后能才",
    "name": "孙永辉",
    "image": "assets/images/yzhg_modul_logoe.png",
    "navigator": "syh"
  },
  {
    "title": "学而不厌 · 诲人不倦",
    "name": "王宇峰",
    "image": "assets/images/yzhg_modul_logoe.png",
    "navigator": "wyf"
  },
];

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: GridView.count(
          crossAxisCount: 2,
          //垂直间距
          mainAxisSpacing: 12,
          //水平间距
          crossAxisSpacing: 12,
          //纵横比
          childAspectRatio: 1,
          children: MODULE_LIST.map((item) {
            return _moduleItem(item);
          }).toList(),
        ));
  }

  Widget _moduleItem(Map<String, String> item) {
    return GestureDetector(
      onTap: () {
        if (item['navigator'] == "yzhg") {
          Navigator.pushNamed(context, YConstant.YZHG_HOME_PAGE);
        } else if (item['navigator'] == "dyc") {
          Navigator.pushNamed(context, DConstant.DYC_MAIN);
        } else if (item['navigator'] == "lyc") {
          Navigator.pushNamed(context, LConstant.LYC_HOME_PAGE);
        } else if (item['navigator'] == "syh") {
          Navigator.pushNamed(context, SConstant.SYH_HOME_PAGE);
        } else if (item['navigator'] == "wyf") {
          Navigator.pushNamed(context, WConstant.WYFG_HOME_PAGE);
        }
      },
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: <Widget>[
          new Image(
              image: new AssetImage(item['image']),
              fit: BoxFit.cover,
              alignment: Alignment.center),
          Positioned(
              bottom: 6,
              left: 12,
              child: Text(
                item['title'],
                style: TextStyle(fontSize: 12, color: Colors.teal),
              )),
          Positioned(
              bottom: 6,
              right: 12,
              child: Text(
                item['name'],
                style: TextStyle(fontSize: 12, color: Colors.red),
              ))
        ],
      ),
    );
  }
}
